﻿using MesBSW.BLL.Mes;
using MesBSW.DAL.MesDB;
using MesBSW.Tools;
using SqlSugar;
using Sunny.UI;
using System;
using System.Collections.Generic;

namespace MesBSW.View.Dialog.Mes
{
    public partial class AddSiteGroupDia : UIEditForm
    {
        #region "内部变量"
        private SiteControler _ctrl = new SiteControler();                  //业务逻辑层
        private SiteGroup _curr_group = null;                               //当前处理的站点组别
        private SqlSugarClient Db = SqlSugares.GetInstance();
        #endregion

        /// <summary>
        /// 无参数构造方法
        /// </summary>
        public AddSiteGroupDia()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="siteGroup"></param>
        public AddSiteGroupDia(SiteGroup siteGroup)
        {
            InitializeComponent();
            this._curr_group = siteGroup;
            this.SetValueIntoControl(this._curr_group);
        }

        #region "控件事件"
        /// <summary>
        /// 新增或者更新站点组别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            SiteGroup tmpBean = this.InitSiteGroupForControl();
            tmpBean.deptId = 13;
            if (tmpBean != null)
            {
                int count = 0;
                try
                {
                    if (tmpBean.ID == 0)
                    {
                        count = this._ctrl.InsertSiteGroup(tmpBean);
                    }
                    else
                    {
                        Boolean isDiff = EntityTools.IsDiffForValue(this._curr_group, tmpBean);
                        string diffStr = EntityTools.CompareForValue(this._curr_group, tmpBean);
                        if (isDiff == false)
                        {
                            string beforeStr = EntityTools.GetPropNameAndValue(this._curr_group);
                            string afterStr = EntityTools.GetPropNameAndValue(tmpBean);
                            count = this._ctrl.UpdateSiteGroup(tmpBean, beforeStr,afterStr);
                        }
                    }
                    if (count > 0)
                    {
                        UIMessageBox.ShowSuccess("站点组别保存成功！");
                    }
                }
                catch (Exception ex)
                {
                    UIMessageBox.ShowError("站点组别保存失败！" + "\r\n" + ex.Message);
                }
            }
            else
            {
                UIMessageBox.ShowError("没有任何可以保存的站点组别，保存失败！");
            }
        }
        #endregion


        #region "内部方法"
        /// <summary>
        /// 根据传入的组别，将组别各信息放到相应的控件中
        /// </summary>
        /// <param name="siteGroup"></param>
        private void SetValueIntoControl(SiteGroup siteGroup)
        {
            if (siteGroup != null)
            {
                this.txtGroupCode.Tag = siteGroup.ID;
                this.txtGroupCode.Text = siteGroup.GroupCode != null ? siteGroup.GroupCode : "";
                this.txtGroupName.Text = siteGroup.GroupName != null ? siteGroup.GroupName : "";
                this.txtRemark.Text = siteGroup.Remark != null ? siteGroup.Remark : "";
                if (siteGroup.IsDel == 1)
                {
                    this.rbnIsDel.Checked = true;
                    this.rbnNotDel.Checked = false;
                }
                else
                {
                    this.rbnIsDel.Checked = false;
                    this.rbnNotDel.Checked = true;
                }

                if (siteGroup.ID <= 0)
                {
                    this.txtGroupCode.Enabled = true;
                }
                else
                {
                    this.txtGroupCode.Enabled = false;
                }
            }
        }

        /// <summary>
        /// 根据界面中各控件的值组成SiteGroup的实体
        /// </summary>
        /// <returns></returns>
        private SiteGroup InitSiteGroupForControl()
        {
            SiteGroup tmpBean = new SiteGroup();
            tmpBean.ID = int.Parse(this.txtGroupCode.Tag.ToString());
            tmpBean.GroupCode = this.txtGroupCode.Text.Trim();
            tmpBean.GroupName = this.txtGroupName.Text.Trim();
            tmpBean.Remark = this.txtRemark.Text.Trim();
            tmpBean.IsDel = 0;
            if (this.rbnIsDel.Checked)
            {
                tmpBean.IsDel = 1;
            }
            return tmpBean;
        }
        #endregion

        
    }
}
